<?php
namespace App\Repositories;

use App\Common\ProductData;
use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;    //引入hash

class GradeConfigRepository
{
    /**
     * 获取爵位列表
     * @return list
     */
    public function GradeConfigList($param)
    {
        $whereArr = [];
        if (isset($param['grade_big_id'])) {
            $whereArr['grade_big_id'] = $param['grade_big_id'];
        }
        $list = app('model_grade_config')->with('reward:id,type')->where($whereArr)->get();
        foreach ($list as $item) {
            if($item->reward) {
                $item->reward->type = $item->type == 1 ? '普通道具' : '特殊道具';
            }
        }
        return $list;
    }

    /*礼包列表*/
    public function getRewardList()
    {
        $where[] = ['active', '=', 1];
        $rewardList = app('model_reward')
            ->select('id','name')
            ->where($where)
            ->orderBy('id', 'desc')
           ->get();
        if ($rewardList) {
            return $rewardList;
        }
        throw new BasicException(98002);
    }

    /**
     * 添加爵位
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function rankAdd($param=[])
    {
        $admin = app('model_grade_config')->create($param);
        if($admin) return $admin;
        throw new BasicException(93010);
    }


    /**
     * 编辑爵位
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function rankSave($param=[])
    {
        $rankSave = app('model_grade_config')->where('id',$param['id'])->update($param);
        if($rankSave!==false) return true;
        throw new BasicException(93011);
    }


    /**
     * 删除爵位
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function rankDel($param=[])
    {
        $rankDel = app('model_grade_config')->whereIn('id',$param)->delete();
        if($rankDel) return $rankDel;
        throw new BasicException(93012);
    }


    /**
     * 爵位等级详情
     * @param $id
     * @return bool
     * @throws BasicException
     */
    public function rankDetail($id)
    {
        $rankDetail = app('model_grade_config')->with('reward:id,type')->where('id',$id)->first();
        if($rankDetail->reward) {
            $rankDetail->reward->type = $rankDetail->reward->type == 1 ? '普通道具' : '特殊道具';
        }

        if($rankDetail) return $rankDetail;
        throw new BasicException(50001);
    }


    /**
     * 获取人气等级列表
     * @param $param
     * @return mixed
     */
    public function GradePopularList($param)
    {
        return app('model_user_popular_level')->select('*')->get()->toArray();
    }

    /**
     * 人气等级新增
     * @param $param
     * @return mixed
     * @throws BasicException
     */
    public function rankPopularAdd($param)
    {
        $admin = app('model_user_popular_level')->create($param);
        if($admin) return $admin;
        throw new BasicException(93010);
    }

    /**
     * 人气等级修改
     * @param $id
     * @param array $param
     * @return bool
     * @throws BasicException
     */
    public function rankPopularSave($id, $param = [])
    {
        $rankSave = app('model_user_popular_level')->where('id',$id)->update($param);
        if($rankSave!==false) return true;
        throw new BasicException(93011);
    }

    /**
     * 人气等级删除
     * @param $ids
     * @return mixed
     * @throws BasicException
     */
    public function rankPopularDel($ids)
    {
        $rankDel = app('model_user_popular_level')->whereIn('id',$ids)->delete();
        if($rankDel) return $rankDel;
        throw new BasicException(93012);
    }

    /**
     * 人气等级详情
     * @param $id
     * @return mixed
     * @throws BasicException
     */
    public function rankPopularDetail($id)
    {
        $rankDetail = app('model_user_popular_level')->where('id',$id)->first();

        if($rankDetail) return $rankDetail;
        throw new BasicException(50001);
    }
}


?>